{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div id="tip_modal">
    <form class="layui-form p-4">
        <h3 class="pb-3">功能菜单/节点</h3>
        <table class="layui-table layui-table-form">

            <tr>
                <td>单据编号</td>
                <td>
                    <input type="text" disabled readonly name="orderNum" class="layui-input" value="{$detail.orderNum|default=''}" required >
                </td>
                <td>单据日期</td>
                <td>
                    <input type="text" readonly name="orderDate" id="orderDate" class="layui-input" value="{$detail.orderDate|default=date('Y-m-d')}">
                </td>

                <td>备注</td>
                <td>
                    <input type="text"  name="remark" class="layui-input" value="{$detail.remark|default=''}">
                </td>
            </tr>
            <tr>
                <td>入库人</td>
                <td>
                    <span>{$detail.username|default=''}</span>
                </td>
                <td>入库时间</td>
                <td><span>{$detail.checkTime|default=''}</span></td>
                <td>状态</td>
                <td><span>{if isset($detail.status)}{$detail.status ==1?'草稿':'已确认'}{else}草稿{/if}</span></td>
            </tr>
            <tr>
                <td colspan="6"><h3>明细</h3></td>
            </tr>
            <tr>
                <td colspan="6">
                    {if !isset($detail.status)||$detail.status==1}
                    <a href="javascript:" onclick="addMx()" class="layui-btn layui-btn-primary">选择产品</a>
                {/if}
                </td>
            </tr>
            <table class="layui-table tableMany">
                <thead>
                <tr>
                    <th>序号</th>
                    <th>品牌</th>
                    <th>品名</th>
                    <th>规格型号</th>
                    <th>订货号</th>
                    <th>单位</th>
                    <th>数量</th>
                    <th>单价</th>
                    <th>操作</th>
                </tr>
                </thead>
                {if isset($mxList)}
                <tbody id="mxTbdy">
                {volist name='mxList' id='ml' key='k'}
                <tr>
                    <td><span class="xuhao">{$k}</span>
                        <input type="hidden" name="mx[mxid][]" value="{$ml.id}">
                        <input type="hidden" class="mx_mdId"  name="mx[mdId][]" value="{$ml.mdId}">
                    <td>{$ml.brand}</td>
                    <td>{$ml.name}</td>
                    <td>{$ml.specs}</td>
                    <td>{$ml.buyCode}</td>
                    <td>{$ml.unit}</td>

                    <td><input type="text" value="{$ml.num}" class="layui-input " maxlength='9' required
                               lay-verify="required" onkeyup="digitalFiltering(this,2)" placeholder="请输入数量"
                               autocomplete="off" min="0" max="999999" name="mx[num][]"></td>

                    <td><input type="text" value="{$ml.sodPrice}" class="layui-input " maxlength='9' required
                               lay-verify="required" onkeyup="digitalFiltering(this,2)" placeholder="请输入单价"
                               autocomplete="off" min="0" max="999999" name="mx[sodPrice][]"></td>
                    <td>
                        {if !isset($detail.status)||$detail.status==1}
                        <a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger"
                           onclick="delMx('{$ml.id}',this)">删除</a>
                        {/if}
                    </td>
                </tr>
                {/volist}
                </tbody>
                {else}
                <tbody id="mxTbdy">
                </tbody>
                {/if}

            </table>
        </table>
        <div class="py-3">
            <input type="hidden" name="id" value="{$id}">
            {if !isset($detail.status)||$detail.status==1}
            <button class="layui-btn layui-btn-normal" type="button" id="sub" lay-submit="" lay-filter="webform1">保存 </button>
            {/if}
            {if isset($detail.status)&&$detail.status==1}
            <button type="button" class="layui-btn layui-btn-normal" onclick="onSure()">确认</button>
            {/if}
            {if isset($detail.status)&&$detail.status==2}
            <button type="button" class="layui-btn layui-btn-danger" onclick="onCancel()">撤销</button>
            {/if}
        </div>
    </form>
</div>

{/block}
<!-- /主体 -->
{block name="script"}
<script>

    const moduleInit = ['tool'];


    function gouguInit() {
        var form = layui.form, tool = layui.tool;
        //监听提交
        form.on('submit(webform1)', function (data) {
            let callback = function (e) {
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            httpRequest('add',data.field,callback,false)
            return false;
        });
layui.laydate.render({
    elem:"#orderDate",
    type:"date"
})


    }
</script>
<script>
    let mxIndex='';
    function addMx() {
        let mxId=[];
        let inputs=document.getElementsByClassName('mx_mdId');
        for (var i = 0; i < inputs.length; i++) {
            // 确保只获取文本或数字类型的输入框值
            mxId.push(inputs[i].value);
        }
        // boxModel("{:url('addMx')}?khId="+khId,'关联报价单','1000')
        boxModel("{:url('addMx')}?mdId="+mxId.join(','),'选择产品','1000')
    }

    function saveAddMx(ojb) {
        var lastListItem = $('.tableMany tr:last');
        let aa = $(lastListItem).find(".xuhao")
        console.log('aaa',ojb)
        layer.close(mxIndex)
        let len = $(aa).text()
        if (!parseInt(len)) {
            len = 0;
        }
        let index = parseInt(len) + 1;
        let str = '';
        for (var i = 0; i < ojb.length; i++) {
            let item = ojb[i];
            str += '      <tr>\n' +
                '                        <td>\n' +
                '                            <span class="xuhao">' + index + '</span>\n' +
                '                            <input type="hidden" name="mx[mxid][]" value="">\n' +
                '                            <input type="hidden" class="mx_mdId" name="mx[mdId][]" value="' + item.id + '">\n' +
                '                        </td>\n' +
                // '                        <td>' + item.bar_code + '</td>\n' +
                '                        <td>' + item.brand + '</td>\n' +
                '                        <td>' + item.name + '</td>\n' +
                '                        <td>' + item.specs + '</td>\n' +
                '                        <td>' + item.buyCode + '</td>\n' +
                '                        <td>' + item.unit + '</td>\n' +
                '                        <td><input type="text" name="mx[num][]" value="" class="layui-input bjNum"  lay-verify="required" oninput="digitalFiltering(this,2)"></td>\n' +
                '                        <td><input type="text" name="mx[sodPrice][]" class="layui-input sodPrice"  lay-verify="required" oninput="digitalFiltering(this,2)"></td>\n' +
                '                        <td><a href="javascript:" class="layui-btn layui-btn-xs layui-btn-danger" onclick="delMx(\'\',this)">删除</a></td>\n' +
                '                    </tr>';
            index++;
        }
        $("#mxTbdy").append(str)

        layui.form.render();
    }



    function removetr(e) {
        $(e).parent().parent().remove()
    }

    function delMx(id = '', e) {
        if (id == '') {
            removetr(e)
            return;
        }
        httpRequest("{:url('removeMx')}",{id: id},function (res) {
            if (res.code != 0) {
                layer.msg(res.msg)
            } else {
                removetr(e)
            }
        },false)
    }


    function onSure() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }

        httpRequest('onSure',{id:id},function (res) {
            layer.msg(res.msg)
            if (res.code == 0) {
                setTimeout(function () {
                    parent.parent.location.reload();
                }, 1000);
            }
        },false)
    }


        function onCancel() {
            let id="{$id}";
            httpRequest('onCancel',{id:id},function (res) {
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },false)
        }
</script>
{/block}
<!-- /脚本 -->